import * as echarts from 'echarts';
var myChart;
export function barLine(data, options, type){//仪表盘
    // var echarts = require('echarts');
    // let  data1= JSON.parse(JSON.stringify(data));
    var series=[];
    var grid = {
        top: '25%',
        left: '3%',
        right: '10%',
        bottom: '6%',
        containLabel: true
    };
    if(options['grid']){
        grid = options['grid'];
    }
    for(var i = 0;i<options.legend.length;i++){
        if(i===0|| type=='bar'){
            series.push({
                name: options.legend[i],
                type: 'bar',
                barWidth: options['barWidth'] || '60%',
                color: options['color'+i] || "",                
                data: data['data'+i] || data,
                itemStyle: options['itemStyle'] || '',
                markLine: options['markLine'] || {},
                stack: options.stack || '',//stack柱状图单向平行
                label: options['label'] || {},    
                barGap: 0,        
                backgroundStyle: options['backgroundStyle'] || '',
                showBackground: options['showBackground'] || '', //柱状图背景颜色true/false
            });
        }else{
            series.push({
                name: options.legend[i],
                type: options.moreType? 'bar': 'line',
                color: options['color'+i] || '',
                data: data['data'+i],
                itemStyle: options['itemStyle'] || '',
                label: options['label'] || {},
                stack: options.stack || '',
                barWidth: options['barWidth'] || '60%',
                showSymbol: options['showSymbol'] || '',
                barGap: 0,            
            });           
        }
       
    }
    var option = {
        title: options['title'] || {},
        backgroundColor: options['backgroundColor']||'',
        dataZoom: options['dataZoom'] || '',
        color: ['#3398DB'],
        // barMinWidth: options['barMinWidth']||'20',
        legend: {
            data: options['legend'],
            top: options['legendTop'] ||'6%',//图例换行居中
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            },
            formatter: options['tooltipFormater']
        },
        grid: grid,
        yAxis: options['yAxis'],
        xAxis: options['xAxis'],
        series: series
    };
    if(!document.getElementById(options['id'])){return false;}
    myChart && myChart.dispose();
    myChart = echarts.init(document.getElementById(options['id']));
	myChart.clear();
	myChart.setOption(option);
}
export default barLine = barLine;

